package com.leet.code.lcof;

import com.algorithm.model.TreeNode;

import java.util.LinkedList;

public class Code37 {

    public static void main(String[] args) {

//        Integer[] s = {4, 2, 6, 1, 3, 5, 7};
//        Integer[] s = {1, 2, null, 3, null, 4, null, 5};
        String sa = "[1,2,null,3,null,4,null,5]";
//        TreeNode t = TreeNodeService.getTreeByArray(s);
//        TreeNodeService.print(t);
        Code37 code37 = new Code37();
        TreeNode st = code37.deserialize(sa);
//        TreeNodeService.print(st);
        String ss = code37.serialize(st);
        st = code37.deserialize(ss);

        System.out.println(ss);
        System.out.println(st);


    }

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if (root == null) {
            return "[null]";
        }

        StringBuilder sb = new StringBuilder("[");
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);

        boolean b = true;
        while (list.size() != 0 && b) {
            int l = list.size();
            b = false;
            for (int i = 0; i < l; i++) {
                TreeNode treeNode = list.removeFirst();

                if (treeNode != null) {
                    sb.append(treeNode.val).append(",");
                    list.add(treeNode.left);
                    list.add(treeNode.right);
                    if (treeNode.left != null || treeNode.right != null) {
                        b = true;
                    }
                } else {
                    sb.append("null,");
                }
            }


        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");

        return sb.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data == null) {
            return null;
        }
        String[] ss = data.substring(1, data.length() - 1).split(",");
        if (ss.length == 0 || ss[0].equals("null")) {
            return null;
        }
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        TreeNode root = new TreeNode(Integer.parseInt(ss[0]));
        treeNodes.add(root);
        int i = 1;
        while (i < ss.length) {
            int size = treeNodes.size();
            for (int s = 0; s < size; s++) {
                TreeNode treeNode = treeNodes.removeFirst();
                if (treeNode == null) {
                    continue;
                }
                if (ss[i].equals("null")) {
                    treeNode.left = null;
                } else {
                    treeNode.left = new TreeNode(Integer.parseInt(ss[i]));
                    treeNodes.add(treeNode.left);
                }
                i++;
                if (i >= ss.length) {
                    break;
                }
                if (ss[i].equals("null")) {
                    treeNode.right = null;
                } else {
                    treeNode.right = new TreeNode(Integer.parseInt(ss[i]));
                    treeNodes.add(treeNode.right);
                }
                i++;

            }
        }

        return root;
    }

}
